#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by master on 2017/3/10


import threading, time
import random


def light():
    if not event.isSet():
        event.set()  # wait就不阻塞 #绿灯状态 相当于设置了一个全局变量
    count = 0
    while True:
        if count < 10:
            print('\033[42;1m--green light on---\033[0m')
        elif count < 13:
            print('\033[43;1m--yellow light on---\033[0m')
        elif count < 20:
            if event.isSet():
                event.clear()  # 清除设定 event变成等待状态
            print('\033[41;1m--red light on---\033[0m')
        else:
            count = 0
            event.set()  # 打开绿灯
        time.sleep(1)
        count += 1


def car(n):
    while 1:
        time.sleep(1)
        if event.isSet():  # 绿灯 判断有没有设定
            print("car [%s] is running.." % n)
        else:
            print("car [%s] is waiting for the red light.." % n)
            event.wait()  # 等待标记被设定 在标记设定之前阻塞


if __name__ == '__main__':
    event = threading.Event()
    Light = threading.Thread(target=light)
    Light.start()
    for i in range(3):
        t = threading.Thread(target=car, args=(i,))
        t.start()
